package com.lsb.blog.controller;

import com.lsb.blog.response.Result;
import com.lsb.blog.utils.QiniuUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

/**
 * @author ：liusibo
 * @description：TODO
 * @date ：2021/8/5 16:38
 */
@RestController
@RequestMapping("/upload")
public class UploadController {

    @Autowired
    private QiniuUtils qiniuUtils;

    @PostMapping
    public Result upload(@RequestParam("image") MultipartFile file){
        //生成新的随机文件名
        String fileName = UUID.randomUUID().toString() + "." + StringUtils.substringAfterLast(file.getOriginalFilename(), ".");
        //上传图片文件 到 七牛云
        boolean upload = qiniuUtils.upload(file, fileName);
        //url: http://qxcxdxqgu.hn-bkt.clouddn.com/050bbc0a-ed3d-4eec-9b71-e6e5589f39d9.jpg
        if (upload){
            return Result.ok().data(QiniuUtils.url + fileName);
        }
        return Result.error().code(20001).message("上传失败");
    }
}
